package com.qiaofei.controller.api;

import com.qiaofei.pojo.Admins;
import com.qiaofei.pojo.PageBean;
import com.qiaofei.pojo.Result;
import com.qiaofei.service.impl.AdminsServiceImpl;
import com.qiaofei.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

/**
 * 管理员管理Controller
 */
@Slf4j
@RestController
@RequestMapping("/api/admins")
public class AdminsController extends BaseController{

    @Autowired
    private AdminsServiceImpl adminsService;

    @GetMapping("/info")
    public Result info(String token){
        Claims claims = JwtUtils.parseJWT(token);
        return Result.success(claims);
    }

    @GetMapping
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       String name, Short state,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
        log.info("分页查询, 参数: {},{},{},{},{},{}",page,pageSize,name,state,begin,end);
        //调用service分页查询
        PageBean pageBean = adminsService.page(page,pageSize,name,state,begin,end);
        return Result.success(pageBean);
    }

    /**
     * 新增
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Admins admins){
        log.info("新增: {}" , admins);
        //调用service新增
        int result = adminsService.add(admins);
        return Result.success(result);
    }

    /**
     * 编辑管理员
     * @param data
     * @return
     */
    @PutMapping
    public Result update(@RequestBody Map<String, Object> data){
        log.info("编辑管理员: {}" , data.toString());
        //调用service新增管理员
        adminsService.update(data);
        return Result.success();
    }

    /**
     * id查询
     * @param ids
     * @return
     */
    @GetMapping("/{ids}")
    public Result getByIds(@PathVariable List<Integer> ids){
        log.info("id查询, id:{}",ids);
        List<Admins> adminsList = adminsService.getByIds(ids);
        return Result.success(adminsList);
    }

    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        log.info("批量删除操作, ids:{}",ids);
        adminsService.deleted(ids);
        return Result.success();
    }
}
